﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
using DevExpress.XtraExport;
using DevExpress.XtraReports.Parameters;
using System.Data.SqlClient;

namespace DMU.Gui.RPT
{
    public partial class ucRPTGiangVien : DevExpress.XtraEditors.XtraUserControl
    {
        Service sv = new Service();
        help hp = new help();
        private DataTable dtRpGv;

        public ucRPTGiangVien()
        {
            InitializeComponent();
        }

        private void gLkpGV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (gLkpGV.Text.Trim() != "")
                {
                    
                    DataTable dt = sv.getDatatable("dmu_proPublicFindGV", new SqlParameter("name", gLkpGV.Text + "%"));
                    gLkpGV.Properties.DataSource = dt;
                    gLkpGV.Properties.ValueMember = "MaGV";
                    gLkpGV.Properties.DisplayMember = "HoTen";
                }

            }
        }

        private void lkpMonHoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (lkpMonHoc.Text.Trim() != "")
                {
                    DataTable dt = sv.getDatatable("dmu_proPublicFindMonHoc", new SqlParameter("name", lkpMonHoc.Text + "%"));
                    lkpMonHoc.Properties.DataSource = dt;
                    lkpMonHoc.Properties.ValueMember = "MaMH";
                    lkpMonHoc.Properties.DisplayMember = "TenMH";
                }

            }
        }

        private void lkpLop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (lkpLop.Text.Trim() != "")
                {
                    DataTable dt = sv.getDatatable("dmu_proPublicFindLop", new SqlParameter("name", lkpLop.Text + "%"));
                    lkpLop.Properties.DataSource = dt;
                    lkpLop.Properties.ValueMember = "MaLop";
                    lkpLop.Properties.DisplayMember = "TenLop";
                }

            }
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            if(gLkpGV.EditValue==null)
            {
                gLkpGV.Focus();
                return;
            }
            if(lkpMonHoc.EditValue==null)
            {
                lkpMonHoc.Focus();
                return;
            }
            if (lkpLop.EditValue == null)
            {
                lkpLop.Focus();
                return;
            }
            try
            {
                SqlParameter[] pr = new SqlParameter[3];
                pr[0] = new SqlParameter("MaGV0", gLkpGV.EditValue.ToString());
                pr[1] = new SqlParameter("MaLop0", lkpLop.EditValue.ToString());
                pr[2] = new SqlParameter("MaMH0", lkpMonHoc.EditValue.ToString());


                DataTable dt = sv.getDatatable("dmu_proRPGiangVien", pr); 
                
                dtRpGv  = dt.Clone();
                dtRpGv.Columns["SoTiet1"].DataType = Type.GetType("System.String");

                if (dt.Rows.Count == 0)
                {
                    hp.showMes("Dữ liệu rỗng !", -1);
                    return;
                }

                foreach (DataRow row in dt.Rows)
                {
                    DataRow r = dtRpGv.NewRow();
                    r["SoTiet1"]=Encoding.UTF8.GetString((byte[])row["SoTiet1"]);
                    r["Ngay"] = row["Ngay"];
                    r["GioBDThuc"] = row["GioBDThuc"];
                    r["GioKTThuc"] = row["GioKTThuc"];
                    r["GioBDTKB"] = row["GioBDTKB"];
                    r["GioKTTKB"] = row["GioKTTKB"];
                    r["NhanXet"] = row["NhanXet"];
                    r["MaMH"] = row["MaMH"];
                    r["MaPhong"] = row["MaPhong"];
                    dtRpGv.Rows.Add(r);
                }
                dtRpGv.AcceptChanges();
                gridControl1.DataSource = dtRpGv;
            }
            catch (Exception et)
            {
                hp.showMes("Có lỗi khi load dữ liệu !. Vui lòng thử lại " + et.Message, -1);
            }
            
        }

        private void gridView4_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {

            rptGV rpt = new rptGV();
            rpt.lblGiangVien.Text = gLkpGV.Text;
            rpt.lblLop.Text = lkpLop.Text;
            rpt.lblMonHoc.Text = lkpMonHoc.Text;
            rpt.bindingSource1.DataSource = dtRpGv;

            rpt.ShowPreviewDialog();

        }

        private void ucRPTGiangVien_Load(object sender, EventArgs e)
        {

        }

        private void lkpLop_EditValueChanged(object sender, EventArgs e)
        {
            if (lkpLop.EditValue != null)
            {
                DataTable dt = sv.getDatatable("dmu_proRPGiangVienLayTen", new SqlParameter("MaLop0", lkpLop.EditValue.ToString()));
                gLkpGV.Properties.DataSource = dt;
                gLkpGV.Properties.ValueMember = "MaGV";
                gLkpGV.Properties.DisplayMember="TenGV";
            }
        }

        private void gLkpGV_EditValueChanged(object sender, EventArgs e)
        {
            if (gLkpGV.EditValue != null)
            {
                SqlParameter[] pr=new SqlParameter[2];
                pr[0]=new SqlParameter("MaLop0", lkpLop.EditValue.ToString());
                pr[1]=new SqlParameter("MaGV0", gLkpGV.EditValue.ToString());
                DataTable dt = sv.getDatatable("dmu_proRPGiangVienLayMHTheoGV",pr);
                lkpMonHoc.Properties.DataSource = dt;
                lkpMonHoc.Properties.ValueMember = "MaMH";
                lkpMonHoc.Properties.DisplayMember = "TenMH";
            }
        }

        private void lkpLop_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                EventArgs et = new EventArgs();
                btnLoadData_Click(sender, et);
            }
        }
    }
}
